﻿using ExpressesClass.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace OFFICE_Helper.Forms
{
    public partial class ChoosePrintNum : Form
    {
        #region 变量
        List<Kddh> kddhs;
        #endregion

        #region 方法
        public ChoosePrintNum(List<Kddh> kddhs)
        {
            InitializeComponent();
            this.kddhs = kddhs.Select(i => (Kddh)i.Clone()).ToList();
        }
        private void InitCtrl()
        {
            dgv_kddh.DataSource = new List<Kddh>();
            dgv_kddh.Columns["mailNo"].Width = 150;
            dgv_kddh.Columns["printNum"].Width = 100;
            dgv_kddh.Columns["printBegin"].Width = 100;
            dgv_kddh.Columns["printEnd"].Width = 100;
            dgv_kddh.Columns["mailNo"].HeaderText = "面单号";
            dgv_kddh.Columns["printNum"].HeaderText = "打印数量";
            dgv_kddh.Columns["printBegin"].HeaderText = "打印开始序号";
            dgv_kddh.Columns["printEnd"].HeaderText = "打印结束序号";
            foreach (DataGridViewColumn col in dgv_kddh.Columns)
            {
                if (col.Name != "mailNo" && col.Name != "printNum" && col.Name != "printBegin" && col.Name != "printEnd")
                {
                    col.Visible = false;
                }
                if (col.Name != "printBegin" && col.Name != "printEnd")
                {
                    col.ReadOnly = true;
                }
            }
        }
        private void Init()
        {
            dgv_kddh.DataSource = this.kddhs;
        }
        private bool Check()
        {
            if (kddhs.Where(k => k.printNum < 1).ToList().Count > 0)
            {
                MessageBox.Show("存在打印数量少于1的条目,请检查.", "提示");
                return false;
            }
            return true;
        }
        #endregion

        #region 事件
        private void ChoosePrintNum_Load(object sender, EventArgs e)
        {
            InitCtrl();
            Init();
        }
        private void btn_print_Click(object sender, EventArgs e)
        {
            dgv_kddh.EndEdit();
            if (Check())
            {
                ChoosePrinter choosePrinter = new ChoosePrinter(kddhs);
                this.DialogResult = choosePrinter.ShowDialog(this);
            }
        }
        private void dgv_kddh_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            dgv_kddh.EndEdit();
            foreach (Kddh k in kddhs)
            {
                k.printNum = k.printEnd - k.printBegin + 1;
            }
            dgv_kddh.Refresh();
        }
        private void dgv_kddh_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (dgv_kddh.Columns[e.ColumnIndex].Name != "printBegin" && dgv_kddh.Columns[e.ColumnIndex].Name != "printEnd")
            {
                return;
            }
            if (int.TryParse(e.FormattedValue.ToString(), out int i))
            {
                e.Cancel = false;
            }
            else
            {
                e.Cancel = true;
                dgv_kddh.CancelEdit();
            }
        }
        #endregion
    }
}
